<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>解绑箱子</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            padding: 20px;
            padding-bottom: 80px;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
        }
        .header {
            text-align: center;
            margin-bottom: 20px;
        }
        .header h1 {
            font-size: 24px;
            color: #333;
        }
        .scan-area {
            background-color: #fff;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .scan-input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }
        .sku-list {
            background-color: #fff;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .sku-item {
            padding: 15px;
            border-bottom: 1px solid #eee;
        }
        .sku-item:last-child {
            border-bottom: none;
        }
        .sku-code {
            font-size: 16px;
            font-weight: bold;
            color: #333;
            margin-bottom: 5px;
        }
        .sku-quantity {
            font-size: 14px;
            color: #666;
        }
        .back-button {
            position: fixed;
            top: 20px;
            left: 20px;
            background-color: #f8f9fa;
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 8px 15px;
            cursor: pointer;
            display: flex;
            align-items: center;
            font-size: 16px;
            color: #333;
            z-index: 100;
        }
        .back-button:before {
            content: "←";
            margin-right: 5px;
            font-size: 18px;
        }
        .confirm-button {
            position: fixed;
            bottom: 0;
            left: 0;
            width: 100%;
            padding: 15px;
            background-color: #ea4335;
            color: #fff;
            border: none;
            font-size: 16px;
            cursor: pointer;
        }
        .confirm-button:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }
        .error-message {
            color: #ea4335;
            font-size: 14px;
            margin-top: 10px;
            display: none;
        }
    </style>
</head>
<body>
    <button class="back-button" onclick="history.back()">返回</button>
    <div class="container">
        <div class="header">
            <h1>解绑箱子</h1>
        </div>
        <div class="scan-area">
            <input type="text" class="scan-input" id="boxCodeInput" placeholder="请扫描箱码" onkeypress="handleScan(event)">
            <div class="error-message" id="errorMessage"></div>
        </div>
        <div class="sku-list" id="skuList"></div>
    </div>
    <button class="confirm-button" id="confirmButton" onclick="handleConfirmUnbind()" disabled>确认解绑</button>

    <script>
        let planCode = null;
        let boxCode = null;
        let skuList = [];

        // 页面加载时获取入库计划单号
        window.onload = function() {
            const urlParams = new URLSearchParams(window.location.search);
            planCode = urlParams.get('code');
            if (!planCode) {
                showError('缺少入库计划单号参数');
                return;
            }
        }

        // 处理扫描事件
        function handleScan(event) {
            if (event.key === 'Enter') {
                const input = document.getElementById('boxCodeInput');
                boxCode = input.value.trim();
                if (!boxCode) return;

                // TODO: 调用API验证箱码是否属于该入库计划
                validateBoxCode(boxCode, planCode);
                input.value = '';
            }
        }

        // 验证箱码
        function validateBoxCode(boxCode, planCode) {
            // 模拟API调用
            // TODO: 替换为实际的API调用
            setTimeout(() => {
                if (boxCode.startsWith('BOX')) {
                    // 模拟成功响应
                    skuList = [
                        { code: 'SKU001', quantity: 10 },
                        { code: 'SKU002', quantity: 5 }
                    ];
                    updateSkuList();
                    document.getElementById('confirmButton').disabled = false;
                    hideError();
                } else {
                    showError('箱码无效或不属于该入库计划');
                    document.getElementById('confirmButton').disabled = true;
                    clearSkuList();
                }
            }, 500);
        }

        // 更新SKU列表
        function updateSkuList() {
            const listContainer = document.getElementById('skuList');
            listContainer.innerHTML = skuList.map(sku => `
                <div class="sku-item">
                    <div class="sku-code">${sku.code}</div>
                    <div class="sku-quantity">数量：${sku.quantity}</div>
                </div>
            `).join('');
        }

        // 清空SKU列表
        function clearSkuList() {
            document.getElementById('skuList').innerHTML = '';
            skuList = [];
        }

        // 显示错误信息
        function showError(message) {
            const errorElement = document.getElementById('errorMessage');
            errorElement.textContent = message;
            errorElement.style.display = 'block';
        }

        // 隐藏错误信息
        function hideError() {
            document.getElementById('errorMessage').style.display = 'none';
        }

        // 处理确认解绑
        function handleConfirmUnbind() {
            if (!boxCode || !planCode) return;

            if (confirm('确定要解绑该箱子吗？此操作将更新入库计划的计划量。')) {
                // TODO: 调用解绑箱子的API
                console.log('解绑箱子:', { boxCode, planCode });
                // 解绑成功后返回列表页
                history.back();
            }
        }
    </script>
</body>
</html> 